package com.xhd.servlet;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xhd.action.Action;
import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.server.ServerConfig;
import com.xhd.util.DataFormat;

public class ClientAction extends Action {
	public static final int TYPE_APP = 1;
	  public static final int TYPE_SCHEDULER = 2;
	  public static final int TYPE_STRUTS = 3;
	  public static final int TYPE_WEB_SERVICE = 4;
	  public static final int CLIENT_PC_APP = 1;
	  public static final int CLIENT_PC_IE = 2;
	  public static final int CLIENT_MOBILE_IE = 3;
	  public static final int CLIENT_PC_AJAX = 4;
	  public static final String COOKIE_SESSION_TIMESTAMP = "sessiontimestamp";
	  public static final String CLIENT_PC_APP_AGENT = "Indy";
	  private int jm516;
	  private String jm517;
	  private String jm518;
	  private String jm519;
	  private long jm520;
	  private String jm521;
	  private int jm522;
	  private int jm285 = 0;
	  private String jm523;
	  private boolean jm524 = false;
	  private boolean jm525 = false;
	  private HashMap jm526;
	  private HashMap jm527;
	  private String jm528;
	  private static SimpleDateFormat jm529 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
	  private static final Log logobj;
	  static Class classobj;

	  static
	  {
	    Class tempclasobj = classobj;
	    if (tempclasobj == null)
	    {
	      try
	      {
	       ClientAction.classobj = Class.forName("com.sinocc.base.server.ClientAction");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    logobj = LogFactory.getLog(tempclasobj);
	  }

	  public ClientAction(int paramInt)
	  {
	    this.jm285 = paramInt;
	  }

	  public int getType()
	  {
	    return this.jm285;
	  }

	  public String getClientIP()
	  {
	    return this.jm518;
	  }

	  public void setClientIP(String paramString)
	  {
	    this.jm518 = paramString;
	  }

	  public void setClientInfo(HttpServletRequest paramHttpServletRequest)
	  {
	    String str1 = paramHttpServletRequest.getHeader(ServerConfig.ProxyClientIPHeader);
	    if (str1 != null)
	      setClientIP(str1);
	    else
	      setClientIP(paramHttpServletRequest.getRemoteAddr());
	    setUserAgent(paramHttpServletRequest.getHeader("User-Agent"));
	    this.jm523 = paramHttpServletRequest.getHeader("Authorization");
	    str1 = paramHttpServletRequest.getHeader("cpccookie");
	    if ((str1 == null) || (str1.length() <= 0))
	      return;
	    String[] arrayOfString = DataFormat.splitString(str1, '&');
	    this.jm526 = new HashMap(arrayOfString.length);
	    for (int j = 0; j < arrayOfString.length; ++j)
	    {
	      String str2 = arrayOfString[j];
	      int i = str2.indexOf(61);
	      if (i <= 0)
	        continue;
	      this.jm526.put(str2.substring(0, i), str2.substring(i + 1));
	    }
	  }

	  public String getLoginGuid()
	  {
	    return this.jm517;
	  }

	  public void setLoginGuid(String paramString)
	  {
	    this.jm517 = paramString;
	  }

	  public void setUserAgent(String paramString)
	  {
	    this.jm519 = paramString;
	  }

	  public long getActionTime()
	  {
	    return this.jm520;
	  }

	  public void setActionTime(long paramLong)
	  {
	    this.jm520 = paramLong;
	  }

	  public void log()
	  {
	    long l = System.currentTimeMillis() - this.jm520;
	    StringBuffer localStringBuffer = new StringBuffer(128);
	    localStringBuffer.append(jm529.format(new Date(this.jm520)));
	    localStringBuffer.append('\t');
	    localStringBuffer.append(this.jm518);
	    localStringBuffer.append('\t');
	    if (getServiceName() == null)
	      localStringBuffer.append("Unknow");
	    else
	      localStringBuffer.append(getServiceName());
	    localStringBuffer.append('\t');
	    if (getMethodName() == null)
	      localStringBuffer.append("Unknow");
	    else
	      localStringBuffer.append(getMethodName());
	    localStringBuffer.append('\t');
	    localStringBuffer.append(l);
	    logobj.action(localStringBuffer.toString());
	  }

	  public void addSubActions(String paramString)
	  {
	    if ((paramString == null) || (paramString.length() <= 0))
	      return;
	    if (this.jm521 == null)
	    {
	      this.jm521 = paramString;
	    }
	    else
	    {
	      ClientAction tmp27_26 = this;
	      tmp27_26.jm521 = tmp27_26.jm521 + "," + paramString;
	    }
	  }

	  public String getSubActions()
	  {
	    return this.jm521;
	  }

	  public int getClientType()
	  {
	    if (this.jm522 != 0)
	      return this.jm522;
	    if (this.jm285 == 1)
	      this.jm522 = 1;
	    else
	      this.jm522 = 2;
	    if ((this.jm519 != null) && (this.jm519.length() > 0))
	    {
	      String str1 = this.jm519.toLowerCase();
	      String str2 = null;
	      if (str1.indexOf("windows ce") >= 0)
	        str2 = "wm";
	      else if (str1.indexOf("android") >= 0)
	        str2 = "android";
	      else if (str1.indexOf("iphone") >= 0)
	        str2 = "iphone";
	      else if (str1.indexOf("ios") >= 0)
	        str2 = "ios";
	      else if (str1.indexOf("symbian") >= 0)
	        str2 = "symbian";
	      else if (str1.indexOf("blackberry") >= 0)
	        str2 = "bb";
	      else if (str1.indexOf("windows phone") >= 0)
	        str2 = "wp";
	      if (str2 != null)
	      {
	        this.jm528 = str2;
	        this.jm522 = 3;
	      }
	      else if (1 == this.jm522)
	      {
	        this.jm528 = "Windows";
	        if (this.jm519.indexOf("Indy") < 0)
	          this.jm522 = 2;
	      }
	      else if (str1.indexOf("linux") >= 0)
	      {
	        this.jm528 = "linux";
	      }
	      else
	      {
	        this.jm528 = "Windows";
	      }
	    }
	    return this.jm522;
	  }

	  public void setClientType(int paramInt)
	  {
	    this.jm522 = paramInt;
	  }

	  public int supportZip()
	  {
	    int i = getClientType();
	    if (1 == i)
	      return 1;
	    if (4 == i)
	      return ((this.jm524) ? 2 : 0);
	    return 0;
	  }

	  public String supportZipName()
	  {
	    int i = getClientType();
	    if (1 == i)
	      return "zip";
	    if (4 == i)
	      return "gzip";
	    return null;
	  }

	  public boolean isOutFlag()
	  {
	    return (1 == getClientType());
	  }

	  public String getAuthorization()
	  {
	    return this.jm523;
	  }

	  public void setAuthorization(String paramString)
	  {
	    this.jm523 = paramString;
	  }

	  public static ClientAction getAppAction(HttpServletRequest paramHttpServletRequest)
	  {
	    ClientAction localClientAction = new ClientAction(1);
	    String str = null;
	    if (paramHttpServletRequest.getHeader("ajax") != null)
	    {
	      localClientAction.setClientType(4);
	      str = paramHttpServletRequest.getHeader("Accept-Encoding");
	      localClientAction.jm524 = ((str != null) && (str.indexOf("gzip") >= 0));
	    }
	    str = paramHttpServletRequest.getParameter("loginguid");
	    if (str == null)
	      str = paramHttpServletRequest.getHeader("loginguid");
	    if (str == null)
	      str = (String)paramHttpServletRequest.getSession().getAttribute("cpcsessionid");
	    if ((str == null) && (4 == localClientAction.getClientType()))
	    {
	      Cookie[] arrayOfCookie = paramHttpServletRequest.getCookies();
	      if (arrayOfCookie != null)
	        for (int i = 0; i < arrayOfCookie.length; ++i)
	        {
	          if (!("CPCSESSIONID".equals(arrayOfCookie[i].getName())))
	            continue;
	          str = arrayOfCookie[i].getValue();
	          break;
	        }
	    }
	    if (str == null)
	      return null;
	    localClientAction.setLoginGuid(str);
	    str = paramHttpServletRequest.getParameter("classid");
	    if (str == null)
	      str = paramHttpServletRequest.getHeader("classid");
	    if (str == null)
	      return null;
	    localClientAction.setServiceName(str);
	    str = paramHttpServletRequest.getParameter("action");
	    if (str == null)
	      str = paramHttpServletRequest.getHeader("action");
	    if (str == null)
	      return null;
	    localClientAction.setMethodName(str);
	    localClientAction.setClientInfo(paramHttpServletRequest);
	    return localClientAction;
	  }

	  public int getProcessId()
	  {
	    return this.jm516;
	  }

	  public void setProcessId(int paramInt)
	  {
	    this.jm516 = paramInt;
	  }

	  public boolean isWeb()
	  {
	    return (1 != getClientType());
	  }

	  public boolean isTrusted()
	  {
	    return ((1 == getClientType()) || (3 == this.jm285));
	  }

	  public boolean isLogProcess()
	  {
	    return this.jm525;
	  }

	  public void setLogProcess(boolean paramBoolean)
	  {
	    this.jm525 = paramBoolean;
	  }

	  public long getSessionTimestamp()
	  {
	    if (this.jm526 == null)
	      return 0L;
	    String str = (String)this.jm526.get("sessiontimestamp");
	    if ((str == null) || (str.length() < 1))
	      return 0L;
	    return Long.parseLong(str);
	  }

	  public void addCookie(String paramString1, String paramString2)
	  {
	    if (this.jm527 == null)
	      if (this.jm526 != null)
	        this.jm527 = ((HashMap)this.jm526.clone());
	      else
	        this.jm527 = new HashMap();
	    this.jm527.put(paramString1, paramString2);
	  }

	  public void outputCookie(HttpServletResponse paramHttpServletResponse)
	  {
	    if ((this.jm527 == null) || (this.jm527.size() <= 0))
	      return;
	    StringBuffer localStringBuffer = new StringBuffer(64);
	    Iterator localIterator = this.jm527.keySet().iterator();
	    while (localIterator.hasNext())
	    {
	      String str = (String)localIterator.next();
	      paramHttpServletResponse.addCookie(new Cookie(str, (String)this.jm527.get(str)));
	      if (localStringBuffer.length() > 0)
	        localStringBuffer.append('&');
	      localStringBuffer.append(str);
	      localStringBuffer.append('=');
	      localStringBuffer.append((String)this.jm527.get(str));
	    }
	    paramHttpServletResponse.setHeader("cpccookie", localStringBuffer.toString());
	  }

	  public String getClientSystem()
	  {
	    return this.jm528;
	  }
}
